import fetch, { BaseResponse, BaseSimpleResponse, loginUrl, RequestContentType } from './index'
import { AddressParam, StuAdd } from '@/types/address/userAdd'
import { OrderConfirm, OrderConfirmParam } from '@/types/orderConfirm'
import { BasePayResponse, PayParam } from '@/types/payCenter'
import { OrderDetailParam } from '@/types/orderList/orderDetail'

/**
 *
 * @param param OrderListParam
 * @returns OrderListRes
 */
export async function orderConfirmIndex(
  param: Partial<OrderConfirmParam>,
): Promise<BaseResponse<OrderConfirm>> {
  const { data } = await fetch.post<BaseResponse<OrderConfirm>>(
    '/api/trade/PC/Order/Confirm/index',
    {
      ...param,
    },
    { contentType: RequestContentType.FORM_URLENCODED },
  )
  return data
}

/**
 * 确定微信支付宝支付
 * @param param PayParam ={payCode: EPaySimpleCode;
                          isUseBalance: number;
                          orderNum: string;} 
 * @returns BasePayResponse
 */
export async function orderPay(param: PayParam): Promise<BasePayResponse> {
  const { data } = await fetch.post<BasePayResponse>(
    '/api/trade/Order/pay',
    {
      ...param,
    },
    { contentType: RequestContentType.FORM_URLENCODED },
  )
  return data
}
/**
 * 取消订单
 * @param param OrderListParam
 * @returns BaseSimpleResponse<string>
 */
export async function cannelPay(param: OrderDetailParam): Promise<BaseSimpleResponse<string>> {
  const { data } = await fetch.post<BaseSimpleResponse<string>>(
    '/api/ocenter/Touch/OrderCancel/index',
    {
      ...param,
    },
    { contentType: RequestContentType.FORM_URLENCODED },
  )
  return data
}

/**
 *支付状态轮训
 * @param param orderNum
 * @returns boolean
 */
export async function getOrderPayStatus(orderNum: string): Promise<boolean> {
  const { data } = await fetch.post<boolean>(
    '/api/trade/order/getOrderPayStatus',
    {
      orderNum: orderNum,
    },
    { contentType: RequestContentType.FORM_URLENCODED },
  )
  return data
}

export const addTradeUrl = '/api/trade/Order/add'
